using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Typing_GUI.CLASS;
namespace Typing_GUI
{
    public partial class frmSignUp : Form
    {
        public frmSignUp()
        {
            InitializeComponent();
        }
        char[] specialChar ={ '+', '-', '.', ',', '/', '?', '"', ':', '\\', ';', '=', '\'', '`', '~', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ', '|' };

        bool KiemTraUsename()
        {
            if (txtUsername.Text.Length == 0)
                return false;
            for (int i = 0; i < txtUsername.Text.Length; i++)
            {
                for (int j = 0; j < specialChar.Length; j++)
                {
                    if (txtUsername.Text[i] == specialChar[j])
                        return false;
                }
            }
            return true;
        }
        public bool KiemTraNgaySinh(DateTime dt)
        {
            if (DateTime.Today.Year - dt.Year > 100 || DateTime.Today.Year-dt.Year<10)
                return false;
            return true;
        }
    
        public bool IsValidEmail(string email)
        {
            try
            {
                int nFirt = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            {
                string pattern = @"^[_a-zA-Z0-9][_.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";
                
                Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
                
                bool valid = false;

                
                if (string.IsNullOrEmpty(email))
                {
                    valid = false;
                }
                else
                {
                
                    valid = check.IsMatch(email.ToLower());
                }
                
                return valid;
            }
        }
        public bool KiemTraNgay(MaskedTextBox mtb)
        {
            if (mtb.MaskFull)
            {
                try
                {
                    DateTime.ParseExact(mtb.Text, "MM/dd/yyyy", null);

                }
                catch
                {
                    MessageBox.Show("invaild date");
                    mtb.ResetText();
                    return false;
                }
            }
            return true;
        }
        public bool KiemTraDangKy()
        {
           
            if (txtUsername.Text == "" ||  txtUsername.Text.Contains(" ")==true)
            {
                MessageBox.Show("Username Can not be empty or Whitespace character !!!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                txtUsername.SelectAll();
                return false; 
            }
            else if (KiemTraUsename() == false)
            {
                MessageBox.Show("Username character special  !!!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                txtUsername.SelectAll();
                return false;
            }
            else if (txtPassword.Text == "" && txtPassword.Text.Length < 6)
            {
                MessageBox.Show("Password must contain at least 6 characters and can not be empty !!!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
                txtPassword.SelectAll();
                return false;
            }
            else if (txtPassword.Text != txtConfirm.Text)
            {

                MessageBox.Show("Cofirm password wrong !! ", "ERROR ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Text = "";
                txtConfirm.Text = "";
                txtPassword.Focus();
                return false;

            }
            else if (txtFullName.Text == "" || txtFullName.Text.Length > 30)
            {

                MessageBox.Show("Name not be blank and less than 30 character  !! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtFullName.Focus();
                return false;

            }
            else if (KiemTraNgaySinh(dtpBirthday.Value) == false)
            {
                MessageBox.Show("Your BirthDay is incorrect \n Min = 10, Max= 100  !! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dtpBirthday.Focus();
                return false;

            }
            else if (txtAddress.Text == "" || txtAddress.Text.Length < 10)
            {
                MessageBox.Show("Address you should fill it  ! At least 10 characters ! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtAddress.Focus();
                return false;

            }
            else if (IsValidEmail(txtEmail.Text) == false)
            {
                MessageBox.Show("Email is wrong !! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtEmail.Focus();
                return false;

            }
            return true;
        }


        public bool KiemTraDuLieu()
        {
            int id = CNguoiDung.GetUserID(txtUsername.Text);
            if (id != -1)
            {
                MessageBox.Show("User name exists ", "ERROR !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.SelectAll();
                return false;
            }
            int ma = CNguoiDung.KiemTraTonTaiEmail(txtEmail.Text);
            if(ma!=-1)
            {
                MessageBox.Show("Email exists ", "ERROR !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.SelectAll();
                return false;
            }
            return true;
        }
        private void MakeVisibleAgain(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }
        private void btnSignup_Click(object sender, EventArgs e)
        {
            if (KiemTraDangKy() == true)
            {
                if (KiemTraDuLieu() == true)
                {
                    CNguoiDung ndung = new CNguoiDung();
                    ndung.Tendangnhap = txtUsername.Text;
                    ndung.Matkhau = txtPassword.Text;
                    ndung.Hoten = txtFullName.Text;
                    ndung.Urlhinhanh = "NewUser.jpg";
                    ndung.Diachi = txtAddress.Text;
                    ndung.Email = txtEmail.Text;
                    ndung.Capdonguoidung = 1;
                   
                    ndung.Bannick = false;
                    ndung.Ngaysinh = dtpBirthday.Value.ToString("dd/MM/yyyy");
                    if (rbMale.Checked == true)
                        ndung.Gioitinh =true;
                    else
                        ndung.Gioitinh = true;
                    ndung.Tongdiem = 0;
                    ndung.Loainguoidung = 2;
                    ndung.createUser();
                    MessageBox.Show("Sign up complete !!!", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmUser frm = new frmUser();
                    frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
                    frm.userID = CNguoiDung.GetUserID(txtUsername.Text);
                    this.Visible = false;
                    frm.Show();
                }
            }
        }


        private void btnClearField_Click(object sender, EventArgs e)
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtConfirm.Text = "";
            txtFullName.Text = "";
            dtpBirthday.Value = DateTime.Today;
            txtAddress.Text = "";
            rbMale.Checked = true;
            txtEmail.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}